// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.controllers;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.property.PropertyCellModifier;
import org.dengues.core.property.PropertyTableViewer;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-9-1 下午02:31:44
 */
public class TablePropertyController extends AbstractPropertyController {

    /**
     * Qiang.Zhang.Adolf@gmail.com TablePropertyController constructor comment.
     * 
     * @param dps
     */
    public TablePropertyController(PropertyTabDynamicSection dps) {
        super(dps);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.property.IPropertyController#createControl(org.eclipse.swt.widgets.Composite,
     * org.dengues.core.components.NodePropertyParameter, org.eclipse.swt.widgets.Control)
     */
    @Override
    public Control createControl(Composite composite, NodePropertyParameter param, Control lastControl) {
        Composite subcomposite = (Composite) super.createControl(composite, param, lastControl);
        PropertyCellModifier propertyCellModifier = new PropertyCellModifier();
        propertyCellModifier.setCommandStack(getCommandStack());
        propertyCellModifier.setParam(param);

        PropertyTableViewer proptableViewer = new PropertyTableViewer(subcomposite, param, !param.isBaseSchema());
        proptableViewer.setCommandStack(getCommandStack());
        proptableViewer.getTableViewer().setCellModifier(propertyCellModifier);
        proptableViewer.initTableInput();
        proptableViewer.setTitle(param.getDisplayName());
        propertyCellModifier.setTableViewer(proptableViewer.getTableViewer());
        Table table = proptableViewer.getTable();
        table.setEnabled(!param.isReadOnly());
        int nbLines = param.getNbLines();
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        if (nbLines > 0) {
            int h = table.getItemHeight() * nbLines;
            gridData.heightHint = h;
        } else {
            gridData = new GridData(GridData.FILL_BOTH);
        }
        table.setLayoutData(gridData);
        curControlMap.put(param.getName(), proptableViewer.getTableViewer());
        return table;
    }

}
